package com.masterclient.app.modules.shop.adapter;

import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.RelativeSizeSpan;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.bumptech.glide.load.Transformation;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.eoner.baselibrary.bean.shop.DealerProductItemBean;
import com.eoner.baselibrary.utils.ScreenUtils;
import com.eoner.baselibrary.utils.Utils;
import com.eoner.baselibrary.utils.ViewTransformUtil;
import com.masterclient.app.R;
import com.masterclient.app.widget.CenterImageSpan;

import jp.wasabeef.glide.transformations.RoundedCornersTransformation;

/**
 * @auther hxm
 * @date 2020/9/11.
 */
public class DealerProductManagerAdapter extends BaseQuickAdapter<DealerProductItemBean, BaseViewHolder> {

    public DealerProductManagerAdapter() {
        super(R.layout.item_dealer_product_manager);
    }

    @Override
    protected void convert(BaseViewHolder helper, DealerProductItemBean item) {

        ViewTransformUtil.glideImageView(mContext, item.getImage(), helper.getView(R.id.item_dealer_manager_image),
                new Transformation[]{new CenterCrop(), new RoundedCornersTransformation(
                        ScreenUtils.dp2px(7), 0)}, R.mipmap.defaults);

        TextView tvName = helper.getView(R.id.item_dealer_manager_name);
        if ("4".equals(item.getProduct_type())) {
            setTextTag(tvName, item.getName(), "礼包");
        } else {
            tvName.setText(item.getName());
        }

        if ("2".equals(item.getProduct_biz_type())){
            //经销品
            helper.setVisible(R.id.view_dealer, true);
            helper.setVisible(R.id.view_other, false);
            Utils.setDin(helper.getView(R.id.item_dealer_manager_total_stock), mContext);
            Utils.setDin(helper.getView(R.id.item_dealer_manager_home_stock), mContext);
            Utils.setDin(helper.getView(R.id.item_dealer_manager_store_stock), mContext);

            helper.setText(R.id.item_dealer_manager_total_stock, item.getTotal_stock())
                    .setText(R.id.item_dealer_manager_home_stock, item.getHome_stock())
                    .setText(R.id.item_dealer_manager_store_stock, item.getStore_stock())
                    .setText(R.id.item_dealer_manager_total_sold, "总销量 " + item.getTotal_sold())
                    .setText(R.id.item_dealer_manager_my_sold, item.getMy_sold())
                    .setText(R.id.item_dealer_manager_price, String.format("¥%s", item.getPrice()))
                    .setText(R.id.item_dealer_manager_desc, !TextUtils.isEmpty(
                            item.getDelivery_name()) ? item.getDelivery_name() : "未添加运费模板")
                    .setTextColor(R.id.item_dealer_manager_desc, Color.parseColor(
                            TextUtils.isEmpty(item.getDelivery_name()) ? "#7399e5" : "#999FAA"))
                    .setGone(R.id.item_dealer_manager_stroke, true);
            helper.addOnClickListener(R.id.view_stock)
                    .addOnClickListener(R.id.item_dealer_manager_desc);

        }else {
            //轻创品
            helper.setGone(R.id.view_dealer, false);
            helper.setVisible(R.id.view_other, true);

            TextView priceView = helper.getView(R.id.item_dealer_other_manager_price);
            Utils.setDin(priceView, mContext);
            Utils.setDin(helper.getView(R.id.item_dealer_other_manager_price_unit), mContext);
            floatSize(item.getPrice(), priceView);
            helper.setText(R.id.item_dealer_other_manager_desc, !TextUtils.isEmpty(
                    item.getDelivery_name()) ? item.getDelivery_name() : "未添加运费模板")
                    .setTextColor(R.id.item_dealer_other_manager_desc, Color.parseColor(
                            TextUtils.isEmpty(item.getDelivery_name()) ? "#ff5000" : "#999FAA"))
                    .setGone(R.id.item_dealer_manager_stroke, false);
            helper.addOnClickListener(R.id.item_dealer_other_manager_desc);
        }
        helper.addOnClickListener(R.id.item_dealer_manager_stroke)
                .addOnClickListener(R.id.item_dealer_manager_shipping)
                .addOnClickListener(R.id.item_dealer_manager_share)
                .addOnClickListener(R.id.item_dealer_manager_preview)
                .addOnClickListener(R.id.item_dealer_manager_image);
    }

    public void floatSize(String content, TextView view) {
        if (!content.contains(".")) {
            view.setText(content);
            return;
        }
        SpannableString spannableString = new SpannableString(content);
        spannableString.setSpan(new RelativeSizeSpan(0.7f), content.indexOf("."), content.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        view.setText(spannableString);
    }

    private void setTextTag(TextView tv, String content, String tag) {
        SpannableString spannableString = new SpannableString(tag + content);
        TextView tvTag = new TextView(mContext);
        tvTag.setText(tag);
        tvTag.setTextSize(11);
        tvTag.setTextColor(mContext.getResources().getColor(R.color.white));
        tvTag.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ScreenUtils.dp2px(16)));
        tvTag.setGravity(Gravity.CENTER);
        tvTag.setPadding(ScreenUtils.dp2px(2), 0, ScreenUtils.dp2px(2), ScreenUtils.dp2px(1));
        tvTag.setBackgroundResource("礼包".equals(tag) ? R.drawable.shape_radius_fe0042 : R.drawable.shape_gradient_ff5c5c);
        tvTag.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
        tvTag.layout(0, 0, tvTag.getMeasuredWidth(), tvTag.getMeasuredHeight());
        tvTag.buildDrawingCache();
        Bitmap bitmap = tvTag.getDrawingCache();
        Drawable d = new BitmapDrawable(bitmap);
        d.setBounds(0, 0, tvTag.getWidth(), tvTag.getHeight());
        CenterImageSpan span = new CenterImageSpan(d, 0, ScreenUtils.dp2px(4));
        spannableString.setSpan(span, 0, tag.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        tv.setText(spannableString);
        tv.setGravity(Gravity.CENTER_VERTICAL);
    }
}
